﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Reflection
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Hello dunso";
            Type t1 = s.GetType();
            Console.WriteLine(t1.FullName);

            Type t2 = Type.GetType("System.String", false, true);
            Console.WriteLine(t2.FullName);

            Type t3 = typeof(string);
            Console.WriteLine(t3.FullName);

            //方法上的反射
            //GetMethods(t1);
            //得到专门的方法
            Console.WriteLine("Join method:{0}", t1.GetMethod("Copy"));

            //通过标志位BindingFalgs缩小方法的范围
            GetMethods(t1, BindingFlags.Public | BindingFlags.Instance);

            //程序集上的反射，动态加载
            Assembly objAssembly;
            objAssembly = Assembly.Load("mscorlib,2.0.0.0,Neutral,b77a5c561934e089");
            Type[] types = objAssembly.GetTypes();

            /*foreach (var t in types)
            {
                Console.WriteLine(t.Name);
            }*/

            //延迟绑定
            objAssembly = Assembly.GetExecutingAssembly();
            Type t4 = objAssembly.GetType("Reflection.Car", false, true);
            object obj = Activator.CreateInstance(t4);
            MethodInfo mmi = t4.GetMethod("IsMoving");
            var isMoving = (bool)mmi.Invoke(obj, null);
            if(isMoving)
            {
                Console.WriteLine("Is Moving");
            }
            else
            {
                Console.WriteLine("Not is moving");
            }


            Console.ReadLine();
        }

        public static void GetMethods(Type t, BindingFlags f)
        {
            MethodInfo[] mi = t.GetMethods(f);
            foreach (MethodInfo m in mi)
            {
                Console.WriteLine("{0}", m.Name);
            }
        }
    }

    public class Car
    {
        public bool InMoving()
        {
            return true;
        }

    }
}
